import {routerRedux} from 'dva/router';
import pathToRegexp from 'path-to-regexp'
import api from '../api';
import moment from 'moment';

export default {
  namespace : 'dailysummaryStrategy',
  state : {
  },
  subscriptions : {
    setup({dispatch, history}) {
      history.listen(({pathname,query}) => {
        if( pathname === '/tradeAnalysis/tca/dailySummaryStrategy' ){
          dispatch({type: 'whetherRequest',payload:query})
        }
      })
    }
  },
  effects : {
    * whetherRequest({
        payload
      }, {call, put,select}) {
        const state=yield select(state => state.dailysummaryStrategy)
        // 看本次的参数和上次的参数是否一致：
        // 一致的话，不请求，不一致的话，正常请求
        // 定义一个对象用于定义参数
        const newObj={}
        for(var key in payload){
          newObj[key]=state[key]
        }
        if(JSON.stringify(payload)!=JSON.stringify(newObj)){
          yield put({
            type:'requestResult',
            payload
          })
        }

    },
    * requestResult({payload}, {call, put,select}){
      const response = yield call(api.tca.getDailysummaryStrategy, payload);
      yield put({type:'clearState',payload:response.data})
      yield put({type: 'querySuccess', payload: response});
    },
    * changeRouter({payload}, {put}){
      yield put(routerRedux.push(payload))
    },

  },
  reducers : {
    clearState(state,{payload}) {
      if(payload.date){
        // 没有结果||查看股票
        return{}
      }else{
        // 查看策略
        return{
          ...state,
          orderNo:null,
          pie:null,
          trendChart:null
        }
      }

    },
    querySuccess(state, {payload}) {

      return {
        ...state,
        ...payload.data
      }
    }
  }
};
